<template>
  <div>
    <div v-for="item in comments" :key="item.uid">
      <div class="commentList">
        <span class="left p1">
          <img src="../../static\img\touxiang.jpg" />
        </span>

        <span class="right p1">
          <div class="rightTop">
            <el-link class="userName" :underline="false">{{item.user.nickName}}</el-link>
            <!-- <el-tag style="height: 30px; margin-left:5px;"></el-tag> -->
            <span class="timeAgo" v-if="item.createTime">{{(item.createTime)}}</span>
          </div>

          <div class="rightCenter" v-html="item.content"></div>
          <div class="rightBottom">
            <el-link class="b1" :underline="false" @click="replyTo(item)">回复</el-link>
            <el-link
              class="b1"
              :underline="false"
              v-if="login.email!=null&&login.email==item.user.email"
              @click="delComment(item.uid)"
            >删除</el-link>
          </div>

          <div class="rightCommentList">
            <!-- 回复框-->
            <div class="comment" :id="item.uid">
              <div class="commentBox">
                <span class="left">
                  <img src="../../static/img/defaultAvatar.png" />
                </span>
                <span class="right">
                  <textarea
                    id="textpanel"
                    class="textArea"
                    placeholder="既然来了，那就留下些什么吧~"
                    v-model="value"
                    @input="vaildCount"
                  ></textarea>
                </span>
              </div>
              <div class="bottom">
                <el-button class="submit p2" type="primary" @click="handleSubmit(item)">发送回复</el-button>
                <el-button class="cancel p2" type="info" @click="handleCancle(item)">取消回复</el-button>
                <div class="emoji-panel-btn p2">
                  <img src="../../static/img/face_logo.png" />
                </div>
                <span class="allow p2">还能输入{{count}}个字符</span>
              </div>
            </div>

            <!--回复渲染-->
            <div>
              <div v-for="reply in item.replyList" :key="reply.uid">
                <div class="commentList">
                  <span class="left p1">
                    <img src="../../static\img\touxiang.jpg" />
                  </span>

                  <span class="right p1">
                    <div class="rightTop">
                      <el-link class="userName" :underline="false">{{reply.user.nickName}}</el-link>
                      <!-- <el-tag style="height: 30px; margin-left:5px;"></el-tag> -->
                      <span class="timeAgo" v-if="reply.createTime">{{(reply.createTime)}}</span>
                    </div>

                    <div class="rightCenter" v-html="reply.content"></div>
                    <div class="rightBottom">
                      <!-- <el-link class="b1" :underline="false" @click="replyTo(item)">回复</el-link> -->
                      <el-link
                        class="b1"
                        :underline="false"
                        v-if="login.email==reply.user.email"
                        @click="delReply(reply)"
                      >删除</el-link>
                    </div>

                    <div class="rightCommentList">
                      <!-- 回复框-->
                      <div class="comment" :id="item.uid">
                        <div class="commentBox">
                          <span class="left">
                            <img src="../../static/img/defaultAvatar.png" />
                          </span>
                          <span class="right">
                            <textarea
                              id="textpanel"
                              class="textArea"
                              placeholder="既然来了，那就留下些什么吧~"
                              v-model="value"
                              @input="vaildCount"
                            ></textarea>
                          </span>
                        </div>
                        <div class="bottom">
                          <el-button
                            class="submit p2"
                            type="primary"
                            @click="handleSubmit(item)"
                          >发送回复</el-button>
                          <el-button class="cancel p2" type="info" @click="handleCancle(item)">取消回复</el-button>
                          <div class="emoji-panel-btn p2">
                            <img src="../../static/img/face_logo.png" />
                          </div>
                          <span class="allow p2">还能输入{{count}}个字符</span>
                        </div>
                      </div>

                      <!--回复渲染-->
                    </div>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCreateTime } from "../util/dateUtil.js";
import {functiontofindIndexByKeyValue} from "../util/ArrayUtil.js"
export default {
  data() {
    return {
      comments: [],
      userTagDictList: [],
      login: {},
      toInfo: {
        uid: "",
        commentUid: ""
      },
      count: 1024,
      value: ""
    };
  },
  methods: {
    replyTo: function(item) {
      if (this.login == null) {
        this.$message.error("登录后才可以进行评论");
        return;
      }
      let userUid = item.userUid;
      let commentUid = item.uid;
      var lists = document.getElementsByClassName("comment");
      for (var i = 0; i < lists.length; i++) {
        lists[i].style.display = "none";
      }
      document.getElementById(commentUid).style.display = "block";
      this.toInfo.commentUid = commentUid;
      this.toInfo.uid = userUid;
    },
    handleCancle(item) {
      let commentUid = item.uid;
      var lists = document.getElementsByClassName("comment");
      for (var i = 0; i < lists.length; i++) {
        lists[i].style.display = "none";
      }
      document.getElementById(commentUid).style.display = "none";
    },
    vaildCount() {
      var count = 1024 - this.value.length;
      if (count <= 0) {
        this.count = 0;
      } else {
        this.count = count;
      }
    },
    delComment(uid) {
      this.$confirm("是否删除此评论", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          for (let i = 0; i < this.comments.length; i++) {
            if (this.comments[i].uid == uid) {
              this.comments.splice(i, 1);
              break;
            }
          }
          let session = window.localStorage;
          session.setItem("comments", JSON.stringify(this.comments));
          this.$message.success("删除成功");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    delReply(reply) {
      this.$confirm("是否删除此回复", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          for (let i = 0; i < this.comments.length; i++) {
            if (this.comments[i].uid == reply.parent) {
              let replys = this.comments[i].replyList;
              let index=functiontofindIndexByKeyValue(replys,"uid",reply.uid);
              replys.splice(index, 1);
              this.comments[i].replyList = replys;
              break;
            }
          }
          let session = window.localStorage;
          session.setItem("comments", JSON.stringify(this.comments));
          this.$message.success("删除成功");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSubmit(item) {
      let session = window.localStorage;
      let login = JSON.parse(session.getItem("login"));
      if (login == null) {
        this.$message.error("登录后才可以进行评论");
      } else {
        if (this.value == "") {
          this.$message.error("回复内容不可以为空");
        } else {
          let comment;
          for (let i = 0; i < this.comments.length; i++) {
            if (this.comments[i].uid == item.uid) {
              comment = this.comments[i];
              let replys = comment.replyList;
              let uid = 1;
              if (replys.length!=0) {
                uid = parseInt(replys[replys.length - 1].uid) + 1;
              }
              let reply = {
                uid,
                content: this.value,
                createTime: getCreateTime(),
                replyList: [],
                source: "about",
                user: login,
                parent:item.uid
              };
              replys.push(reply);
              this.comments[i].replyList = replys;
              session.setItem("comments", JSON.stringify(this.comments));
              this.$message.success("发表回复成功");
              document.getElementById(item.uid).style.display = "block";
              break;
            }
          }
        }
      }
    }
  },
  mounted() {
    let session = window.localStorage;
    let comments = JSON.parse(session.getItem("comments"));
    this.comments = comments == null ? [] : comments;
    this.login = JSON.parse(session.getItem("login"));
    if (this.login == null) this.login = {};
  }
};
</script>

<style>
.commentStyle {
  display: block;
  margin-top: 10px;
  margin-left: 10px;
  border-left: 1px dashed SlateGray;
}
.comment {
  display: none;
}
.commentList {
  width: 100%;
  margin: 0 auto;
}
.commentList .p1 {
  float: left;
}
.commentList .left {
  display: inline-block;
  width: 5%;
  height: 100%;
}
.commentList .left img {
  margin: 0 auto;
  width: 100%;
  border-radius: 50%;
}
.commentList .right {
  display: inline-block;
  width: 93%;
  margin-left: 5px;
}
.commentList .rightTop {
  height: 30px;
  margin-top: 2px;
}
.commentList .rightTop .userName {
  color: #303133;
  margin-left: 10px;
  font-size: 16px;
  font-weight: bold;
}
.commentList .rightTop .timeAgo {
  color: #909399;
  margin-left: 10px;
  font-size: 15px;
}
.commentList .rightCenter {
  margin-left: 20px;
  height: 50px;
  margin-top: 15px;
}
.commentList .rightBottom {
  margin-left: 10px;
  height: 30px;
}
.commentList .rightBottom el-link {
}
.commentList .rightBottom .b1 {
  margin-left: 10px;
}
</style>